package com.bizhongcaip.seller.ui.welcome;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import com.alibaba.fastjson.JSONObject;
import com.sobot.chat.SobotApi;
import android.widget.ImageView;
import net.caixiaomi.info.MainActivity;
import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.ConfigQueryModel;
import net.caixiaomi.info.model.QueryUserNoticeModel;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.info.util.StatusBarUtil;
import butterknife.BindView;
import retrofit2.Call;
import net.caixiaomi.info.interfaces.SimpleEqualRatioTarget;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import net.caixiaomi.info.helper.AppHelper;
import com.bizhongcaip.seller.ui.welcome.GuideActivity;


public class SplashActivity extends BaseActivity {
    @BindView(R.id.image)
    ImageView mImage;
    @Override
    protected int layout() {
        return R.layout.act_splash;
    }

    private CountDownTimer mCountDownTimer = new CountDownTimer(2 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            boolean firstOpen = CommonApp.STORE.getBoolean(Constants.KEY_FIRST_OPEN, true);
            if (firstOpen) {
                toGuide();
            } else {
                toHome();
            }
            onBackPressed();
        }
    };

    private void toHome() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void toGuide() {
        Intent intent = new Intent(this, GuideActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarColor(this, R.color.transparent);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        RequestOptions options = new RequestOptions().centerCrop();
        int width = AppHelper.getScreenWidth(CommonApp.getInstance());
        int height = AppHelper.getScreenHeight(CommonApp.getInstance());
        Glide.with(this).asBitmap().apply(options).load(R.mipmap.ic_splash).into(new SimpleEqualRatioTarget(mImage, width, height));

        mCountDownTimer.start();
        loadConfig();
        if (!TextUtils.isEmpty(CommonApp.getToken())) {
            queryUserNotice();
        }
        SobotApi.initSobotSDK(this, getString(R.string.SOBOT_KEY), "");
        Intent intent = getIntent();
        Uri uri=intent.getData();
        if(uri==null&&!isTaskRoot()) {
            finish();
            return;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCountDownTimer.cancel();
        mCountDownTimer = null;
    }

    private void loadConfig() {
        JSONObject params = new JSONObject();
        params.put("str", String.valueOf(System.currentTimeMillis()));
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<ConfigQueryModel>> configQuery = requestService.configQuery(params);
        configQuery.enqueue(new ResponseCallback<BaseCallModel<ConfigQueryModel>>() {
            @Override
            public void onSuccess(BaseCallModel<ConfigQueryModel> data) {
                ConfigQueryModel model = data.data;
//                CommonApp.STORE.set(Constants.KEY_DEAL_VERSION, false);
                CommonApp.STORE.set(Constants.KEY_DEAL_VERSION, TextUtils.equals(model.getTurnOn(), "1"));
            }

            @Override
            public void onFail(ResponseError error) {
                if (error.getCode() == 301030) {
                    CommonApp.STORE.set(Constants.KEY_DEAL_VERSION, false);
                }
            }

            @Override
            public void onError() {
            }
        });
    }

    private void queryUserNotice() {
        JSONObject params = new JSONObject();
        params.put("str", String.valueOf(System.currentTimeMillis()));
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<QueryUserNoticeModel>> baseCallModelCall = requestService.queryUserNotice(params);
        baseCallModelCall.enqueue(new ResponseCallback<BaseCallModel<QueryUserNoticeModel>>() {
            @Override
            public void onSuccess(BaseCallModel<QueryUserNoticeModel> data) {
                QueryUserNoticeModel model = data.data;
                CommonApp.STORE.set(Constants.KEY_MESSAGE_NOTICE, model.getMessageNotice() != 0);
                CommonApp.STORE.set(Constants.KEY_BONUS_NOTICE, model.getBonusNotice() != 0);
            }

            @Override
            public void onFail(ResponseError error) {
            }

            @Override
            public void onError() {
            }
        });
    }


}
